package com.zty.xiaomi.server.Service.ServiceOrder;

import com.zty.xiaomi.server.Entity.Service.ServiceOrder;
import com.zty.xiaomi.server.Mapper.ServiceOrderMapper;
import com.zty.xiaomi.server.Service.Order.OrderServiceImp;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.io.IOException;
@Service
@Transactional
public class ServiceOrderServiceImp implements ServiceOrderService {

    @Autowired
    private ServiceOrderMapper serviceOrderMapper;

    @Autowired
    private OrderServiceImp orderServiceImp;

    @Override
    public int addServiceOrder(ServiceOrder serviceOrder) throws IOException {
        int num = serviceOrderMapper.addServiceOrder(serviceOrder);
        if (num != 1) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        } else {
            num = orderServiceImp.updateOrderGoodStatusByOrderNoAndGoodId(serviceOrder.getOrderNo()
                    ,serviceOrder.getGood_id(),1);
            if (num != 1) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            }
        }
        return num;
    }

    @Override
    public ServiceOrder getServiceOrderDetailByOrderNoAndGoodId(int orderNo, int good_id) throws IOException {
        return serviceOrderMapper.getServiceOrderDetailByOrderNoAndGoodId(orderNo, good_id);
    }
}
